<?php

class Lib_Yar extends Yar_Client
{
    private static $env;

    public function execute($method, $params)
    {
        if (!self::$env) {
            self::$env = getenv('RUNTIME_ENVIROMENT') ? getenv('RUNTIME_ENVIROMENT') : 'local';
        }
        try {
            $res = call_user_func_array(array($this, $method), $params);
        } catch (Yar_Client_Exception $e) {
            if (self::$env == 'local' || self::$env == 'test') {
                echo $e->getType();
                echo '<br>';
                echo $e->getMessage();
                echo '<br>';
                echo $e->getPrevious();
                echo '<br>';
                echo $e->getFile();
                echo '<br>';
                echo $e->getLine();
                echo '<br>';
                echo $e->getTraceAsString();
                echo '<br>';
            } else {
                $logs = 'type:'.$e->getType();
                $logs .= 'message:'.$e->getMessage();
                $logs .= 'message:'.$e->getMessage();
                $logs .= 'previous:'.$e->getPrevious();
                $logs .= 'file:'.$e->getFile();
                $logs .= 'line:'.$e->getLine();
                $logs .= 'trace:'.$e->getTraceAsString();
                Func_Log::write('cpas', 'yar', $logs);
                return ['code'=>500,"message"=>$e->getMessage()];
                exit;
            }

            return false;
        }

        return $res;
    }
}
